package com.solcoo.merchant.util;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;

import com.solcoo.common.util.StringUtil;

public class DialogUtil {
	private static final String TAG = "DialogUtil";

	public static Dialog createDialog(Context context,
			OnClickListener positiveListener,
			OnClickListener onNegativeListener,
			OnDismissListener onDismissListener, String message, String title) {
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
		if (StringUtil.isNotBlank(message)) {
			dialogBuilder.setMessage(message);
		}
		if (StringUtil.isNotBlank(title)) {
			dialogBuilder.setTitle(title);
		}
		if (positiveListener == null) {
			positiveListener = onNegativeListener;
		}
		dialogBuilder.setPositiveButton("确定", positiveListener);
		if (onNegativeListener != null) {
			dialogBuilder.setNegativeButton("取消", onNegativeListener);
		}
		Dialog dialog = dialogBuilder.create();
		if (onDismissListener != null) {
			dialog.setOnDismissListener(onDismissListener);
		}
		return dialog;
	}

	public static OnClickListener onNegativeListener = new OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			dialog.dismiss();
		}
	};

	public static void dimissDialog(Dialog dialog) {
		if (dialog != null && dialog.isShowing())
			dialog.dismiss();
	}

}
